import os
import util
import wx

class Image(object):
    def __init__(self, image):
        self.image = image
        
class Sound(object):
    def __init__(self, sound, is_language=True):
        self.sound = sound
        self.is_language = is_language
        
class Text(object):
    def __init__(self, text, phonetic):
        self.text = text
        self.phonetic = phonetic

class Concept(object):
    def __init__(self, name):
        self.name = name
        self.associates = []
        
    def add_associate(self, associate):
        self.associates.append(associate)
        
    @property
    def images(self):
        return filter(lambda x: x.__class__ == Image, self.associates)
        
    @property
    def sounds(self):
        return filter(lambda x: x.__class__ == Sound, self.associates)
    
    @property
    def texts(self):
        return filter(lambda x: x.__class__ == Text, self.associates)
    
    @property
    def phonetic_text(self):
        for text in self.texts:
            if text.phonetic: return text
        return None
        
def load(path):
    whole_name = os.path.split(path)[-1]
    chinese, pinyin = whole_name.split(' - ')
    concept = Concept(chinese)
    concept.associates.append(Text(chinese, phonetic=False))
    concept.associates.append(Text(pinyin, phonetic=True))
    for filename in os.listdir(path):
        file_fullpath = os.path.join(path, filename)
        name, ext = os.path.splitext(filename.lower())
        # image
        if ext in ('.jpg', '.bmp', '.png', '.gif'):
            concept.add_associate(Image(wx.Image(file_fullpath)))
        elif ext == '.mp3':
            concept.add_associate(Sound(file_fullpath)) 
    return concept

def main():
    concept_parent_folder = os.path.join(util.resource_folder, u"concept")
    for concept_folder in os.listdir(concept_parent_folder):
        if "." in concept_folder: continue
        concept = load(os.path.join(concept_parent_folder, concept_folder))
        print concept.name
        print concept.images
        print concept.sounds
        print concept.texts
        
        break
    
    

if __name__ == '__main__': 
    main()